package com.treasure.mall.zbqmallmanager.controller.pay;

import com.treasure.mall.biz.dto.condition.PaymentBillCondition;
import com.treasure.mall.biz.dto.condition.RefundBillCondition;
import com.treasure.mall.biz.entity.pay.PaymentBillPO;
import com.treasure.mall.biz.entity.pay.RefundBillPO;
import com.treasure.mall.biz.service.core.pay.PaymentBillService;
import com.treasure.mall.biz.service.core.pay.RefundBillService;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/14
 */
@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private PaymentBillService paymentBillService;
    @Autowired
    private RefundBillService refundBillService;

    @GetMapping("/payment/list")
    public String paymentBills(
            @ModelAttribute("condition") PaymentBillCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<PaymentBillPO> orderPOS = paymentBillService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", orderPOS.getTotalElements());
        model.addAttribute("totalPages", orderPOS.getTotalPages());
        model.addAttribute("bills", orderPOS.getContent());

        return "pay/payment_bill_list";
    }

    @GetMapping("/refund/list")
    public String refundBillList(
            @ModelAttribute("condition") RefundBillCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<RefundBillPO> orderPOS = refundBillService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", orderPOS.getTotalElements());
        model.addAttribute("totalPages", orderPOS.getTotalPages());
        model.addAttribute("bills", orderPOS.getContent());

        return "pay/refund_bill_list";
    }
}
